/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaReqRTC1.java
 *
 * Created on 02/10/2012, 18:56:54
 */
package meuteste;

import java.io.IOException;

/**
 *
 * @author Laboratório
 */
public class TelaReqRTC1 extends javax.swing.JFrame {

    private PortaSerial portaSerial;
    private TelaGerenteSemResposta telaGerenteSemResposta;
    private TelaReqRTC2 telaReqRTC2;
    private TelaSemRotaGerente telaSemRotaGerente;
    private TelaErroNoFormatoDoProtocolo telaErroNoFormatoDoProtocolo;
    private TelaCoordenadorOcupado telaCoordenadorOcupado;
    
    /**
     * Creates new form TelaReqRTC1
     */
    public TelaReqRTC1(PortaSerial portaSerial, TelaGerenteSemResposta telaGerenteSemResposta, TelaSemRotaGerente telaSemRotaGerente, TelaErroNoFormatoDoProtocolo telaErroNoFormatoDoProtocolo, TelaCoordenadorOcupado telaCoordenadorOcupado) {
        initComponents();
        this.portaSerial = portaSerial;
        this.telaGerenteSemResposta = telaGerenteSemResposta;
        telaReqRTC2 = new TelaReqRTC2();
        this.telaSemRotaGerente = telaSemRotaGerente;
        this.telaErroNoFormatoDoProtocolo = telaErroNoFormatoDoProtocolo;
        this.telaCoordenadorOcupado = telaCoordenadorOcupado;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelReqRTC1 = new javax.swing.JLabel();
        textFieldTelaReqRTC = new javax.swing.JTextField();
        botaoTelaReqRTC = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        labelReqRTC1.setText("Digite o gerente que deseja requisitar o RTC");

        botaoTelaReqRTC.setText("Ok!");
        botaoTelaReqRTC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                botaoTelaReqRTCMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(textFieldTelaReqRTC, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botaoTelaReqRTC, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelReqRTC1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelReqRTC1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textFieldTelaReqRTC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoTelaReqRTC)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoTelaReqRTCMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoTelaReqRTCMouseReleased
        this.setVisible(false);
        boolean recebiResposta = false;
        while(!recebiResposta) {
            try {
                String resposta = portaSerial.reqRTC(new StringBuilder(this.textFieldTelaReqRTC.getText()));
                switch (resposta) {
                    case "iR?t":
                        telaSemRotaGerente.setVisible(true);
                        recebiResposta = true;
                        break;
                    case "iG?t":
                        telaGerenteSemResposta.setVisible(true);
                        recebiResposta = true;
                        break;
                    case "iP?t":
                        telaErroNoFormatoDoProtocolo.setVisible(true);
                        recebiResposta = true;
                        break;
                    case "":
                        System.out.println("Não veio resposta");
                        break;
                    case "iiiiT?t":
                        telaCoordenadorOcupado.setVisible(true);
                        recebiResposta = true;
                        break;
                    default:
                        if (!resposta.matches("iX.*")) {
                            StringBuilder stb = new StringBuilder(resposta);
                            telaReqRTC2.getLabelTelaReqRTC2().setText("O horario gravado no RTC do gerente " + stb.substring(1, stb.length() - 1));
                            telaReqRTC2.setSize(telaReqRTC2.getLabelTelaReqRTC2().getWidth() + 10, telaReqRTC2.getLabelTelaReqRTC2().getHeight() + 10);
                            telaReqRTC2.pack();
                            telaReqRTC2.setVisible(true);
                            recebiResposta = true;
                        } else {
                            System.out.println("=== Aguardando ===");
                            Thread.sleep(35000);
                        }
                        break;
                }
            } catch (IOException | InterruptedException ex) {
            }
        }
        textFieldTelaReqRTC.setText("");
    }//GEN-LAST:event_botaoTelaReqRTCMouseReleased
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //    new TelaReqRTC1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoTelaReqRTC;
    private javax.swing.JLabel labelReqRTC1;
    private javax.swing.JTextField textFieldTelaReqRTC;
    // End of variables declaration//GEN-END:variables
}
